package siga.visao_busca;

import java.sql.SQLException;
import siga.banco.unimestre;
import siga.visao.index;
import siga.visao.verErros;

public class buscaVer_uni extends javax.swing.JDialog {
    static String tipo;
    static String getTipo() { return tipo; }
    static void setTipo(String Tipo) {tipo = Tipo;}

    public static boolean ConfirmaBusca;
    public static boolean isConfirmaBusca() {
        return ConfirmaBusca;
    }
    public static void setConfirmaBusca(boolean Confirma) {
        ConfirmaBusca = Confirma;
    }

    public buscaVer_uni(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelLocalizar = new javax.swing.JPanel();
        jtpPrincipal = new javax.swing.JTabbedPane();
        jpPessoas = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfCodigo = new javax.swing.JTextField();
        jtfDescricao = new javax.swing.JTextField();
        jbLocalizar = new javax.swing.JButton();
        painelResultado = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Pessoas");
        setName("fBusca"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setText("Nome :");

        jLabel2.setText("Código :");

        jbLocalizar.setText("Localizar");
        jbLocalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLocalizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpPessoasLayout = new javax.swing.GroupLayout(jpPessoas);
        jpPessoas.setLayout(jpPessoasLayout);
        jpPessoasLayout.setHorizontalGroup(
            jpPessoasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpPessoasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbLocalizar)
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jpPessoasLayout.setVerticalGroup(
            jpPessoasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpPessoasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpPessoasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbLocalizar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtpPrincipal.addTab("Pessoas", new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/Business_People_16x16.png")), jpPessoas); // NOI18N

        javax.swing.GroupLayout painelLocalizarLayout = new javax.swing.GroupLayout(painelLocalizar);
        painelLocalizar.setLayout(painelLocalizarLayout);
        painelLocalizarLayout.setHorizontalGroup(
            painelLocalizarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 773, Short.MAX_VALUE)
        );
        painelLocalizarLayout.setVerticalGroup(
            painelLocalizarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selecionaLinha(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(60);

        javax.swing.GroupLayout painelResultadoLayout = new javax.swing.GroupLayout(painelResultado);
        painelResultado.setLayout(painelResultadoLayout);
        painelResultadoLayout.setHorizontalGroup(
            painelResultadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 773, Short.MAX_VALUE)
        );
        painelResultadoLayout.setVerticalGroup(
            painelResultadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelResultadoLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelLocalizar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(painelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(painelLocalizar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selecionaLinha(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selecionaLinha
        if(evt.getClickCount() == 2){
            setConfirmaBusca(true);
            index.LRpessoa.add(0,jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString());
            index.LRpessoa.add(1,jTable1.getValueAt(jTable1.getSelectedRow(), 1).toString());
            setVisible(false);
            dispose();
        }
    }//GEN-LAST:event_selecionaLinha

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        jtfDescricao.requestFocus();
    }//GEN-LAST:event_formWindowActivated

    private void jbLocalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLocalizarActionPerformed
        int i = jTable1.getRowCount();
        String txtsql;
        javax.swing.table.DefaultTableModel jtm = (javax.swing.table.DefaultTableModel)jTable1.getModel();
        for(int x=(i-1);x>=0; --x ){
            jtm.removeRow(x);
        }
        try {
            unimestre.Conexao();
            if(!jtfCodigo.getText().isEmpty()){
                txtsql = "and cd_pessoa="+jtfCodigo.getText();
            }else if(!jtfDescricao.getText().isEmpty()){
                txtsql = "and nm_pessoa like \""+jtfDescricao.getText()+"%\"";
            }else{
                txtsql = "";
            }
            unimestre.Query("select cd_pessoa as codigo, nm_pessoa as descricao from pessoas where sn_requerimentos_email = \"S\" "+txtsql+" order by nm_pessoa");
            while (unimestre.rs.next()) {
                jtm.addRow(new Object[]{unimestre.rs.getString("codigo"),unimestre.rs.getString("descricao")});
            }
            unimestre.FechaConexao();
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
}//GEN-LAST:event_jbLocalizarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbLocalizar;
    private javax.swing.JPanel jpPessoas;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfDescricao;
    private javax.swing.JTabbedPane jtpPrincipal;
    private javax.swing.JPanel painelLocalizar;
    private javax.swing.JPanel painelResultado;
    // End of variables declaration//GEN-END:variables

}
